import { ResultType } from '@/types/index'
import {
  getInstitutionList,
  getGradeInfo,
  deleteSchoolInfo,
  saveSchoolInfo,
  getAreaInfo,
  getSchoolInfoByCode
} from '@/api'
import { Reducer, Effect } from 'umi'

export interface InstitutionListType {
  // 机构编号
  code: string
  //   序号
  index: number
  //   学校名称
  name: string
  //   学段
  grade: string
}

export interface OrganizationModelState {
  institutionList: InstitutionListType[]
  gradeList: any[]
  areaList: any[]
  schoolInfo: any
}
interface OrganizationModelType {
  namespace: string
  state: OrganizationModelState
  reducers: {
    setState: Reducer<OrganizationModelState>
  }
  effects: {
    function_getInstitutionList: Effect
    function_getGradeInfo: Effect
    function_deleteSchoolInfo: Effect
    function_saveSchoolInfo: Effect
    function_getAreaInfo: Effect
    function_getSchoolInfoByCode: Effect
  }
}
export default <OrganizationModelType>{
  namespace: 'organizationModel',
  state: { institutionList: [], gradeList: [], areaList: [], schoolInfo: {} },
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *function_getInstitutionList({ payload: { grade, name } }, { call, put }) {
      const result: ResultType = yield call(getInstitutionList, { grade, name })
      yield put({
        type: 'setState',
        payload: {
          institutionList: result.obj
        }
      })
    },
    // 查询学段
    *function_getGradeInfo({}, { call, put }) {
      const result: ResultType = yield call(getGradeInfo)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            gradeList: result.obj
          }
        })
      }
    },
    *function_deleteSchoolInfo({ payload: { code }, callback }, { call }) {
      const result: ResultType = yield call(deleteSchoolInfo, code)
      callback(result)
    },
    *function_saveSchoolInfo(
      { payload: { code, name, grade, areaCode }, callback },
      { call }
    ) {
      const result: ResultType = yield call(saveSchoolInfo, {
        code,
        name,
        grade,
        areaCode
      })
      callback(result)
    },
    *function_getAreaInfo(_, { call, put }) {
      const result: ResultType = yield call(getAreaInfo)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            areaList: result.obj
          }
        })
      } else {
        yield put({
          type: 'setState',
          payload: {
            areaList: []
          }
        })
      }
    },
    *function_getSchoolInfoByCode({ payload: { code } }, { call, put }) {
      const result: ResultType = yield call(getSchoolInfoByCode, code)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            schoolInfo: result.obj
          }
        })
      } else {
        yield put({
          type: 'setState',
          payload: {
            schoolInfo: {}
          }
        })
      }
    }
  }
}
